package com.tcwlkj.uuService.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.adapter.RecommentAdapter;
import com.tcwlkj.uuService.adapter.ShareAdapter;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.ShareModel;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.MyRefresh;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/9/30 0030.
 */

public class RecommentActivity extends BaseActivity{
    @Bind(R.id.iv_qr_code)
    ImageView iv_qr_code;
    @Bind(R.id.tv_recommend_content)
    TextView tv_recommend_content;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.refresh)
    TwinklingRefreshLayout refresh;
    @Bind(R.id.tv_title)
    TextView tv_title;
    @Bind(R.id.share_recycler)
    RecyclerView share_recycler;
    @Bind(R.id.tv_money)
    TextView tv_money;
    @Bind(R.id.ll_title)
    LinearLayout ll_title;

    private List<ShareModel.Details> modelList_Details = new ArrayList<ShareModel.Details>();
    private RecommentAdapter recommentAdapter=new RecommentAdapter(modelList_Details);

    private List<ShareModel.Children> modelList_Children = new ArrayList<ShareModel.Children>();
    private ShareAdapter shareAdapter=new ShareAdapter(modelList_Children,this);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recomment);
        ButterKnife.bind(this);
        changeTitle("邀请");
        initview();
        ShareContent();
    }
    private void initview() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        share_recycler.setLayoutManager(linearLayoutManager);
        share_recycler.setAdapter(shareAdapter);

        LinearLayoutManager fullyLinearLayout=new LinearLayoutManager(this);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(fullyLinearLayout);
        recyclerView.setFocusable(false);
        recyclerView.setAdapter(recommentAdapter);
        MyRefresh.getInstance().getMusic(refresh,false);
        refresh.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                ShareContent();
                refresh.finishRefreshing();
            }
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                ShareContent();
                refresh.finishLoadmore();
            }
        });
    }
    int i=0;
    private void ShareContent() {
        Call<CommonResponse<ShareModel>> httpResultCall = RetrofitUtils.getRequest(this, false).share(PreferencesUtils.getString(MyApp.getContext(), "userID"),"13");
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<ShareModel>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<ShareModel> commonResponse= (CommonResponse<ShareModel>) t;
                ShareModel shareModel=commonResponse.getData();
                modelList_Details=shareModel.getDetails();
                recommentAdapter.updateData(modelList_Details);
                modelList_Children=shareModel.getChildren();
                shareAdapter.updateData(modelList_Children);
                Glide.with(RecommentActivity.this).load(shareModel.getLogo()).into(iv_qr_code);
                tv_title.setText(shareModel.getTitle());
                tv_recommend_content.setText(URLDecoder.decode(shareModel.getDesc()));
                double a=0d;
                for(int i=0;i<modelList_Details.size();i++){
                    a=a+Double.parseDouble(modelList_Details.get(i).getRURValue());
                }
                if(modelList_Details.size()==0){
                    tv_money.setVisibility(View.GONE);
                    ll_title.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.GONE);
                }else {
                    tv_money.setVisibility(View.VISIBLE);
                    ll_title.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);
                }
                String s="您已赢取"+a+"元推荐奖励";
                tv_money.setText(s);
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    ShareContent();
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }
    protected boolean isBindEventBusHere() {
        return true;
    }

    protected void onEventComing(EventCenter eventCenter) {
        if(eventCenter.getEventCode()==1011){
            checkPermission(new CheckPermListener() {
                    @Override
                    public void superPermission() {
                        Intent intent=new Intent(RecommentActivity.this,SMSActivity.class);
                        startActivityForResult(intent, (Integer) eventCenter.getData());
                    }
                },R.string.ACCOUNTS,Manifest.permission.GET_ACCOUNTS);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode,resultCode,data);//完成
        if(resultCode==2){
            String phone=data.getExtras().getString("phone");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra("address", phone);
            intent.putExtra("sms_body", URLDecoder.decode(modelList_Children.get(requestCode).getDesc())+modelList_Children.get(requestCode).getUrl());
            intent.setType("vnd.android-dir/mms-sms");
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
        }
    };

}
